/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import Modelo.Producto;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Locale;
import jxl.*;

/**
 *
 * @author Damian
 */
public class ControlExcel {

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    public boolean csv(String dir) {
        try {
            //File to store data in form of CSV
            File f = new File("input.csv");

            OutputStream os = (OutputStream) new FileOutputStream(f);
            String encoding = "UTF8";
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            //Excel document to be imported
            String filename = dir;
            WorkbookSettings ws = new WorkbookSettings();
            ws.setLocale(new Locale("es", "ES"));
            Workbook w = Workbook.getWorkbook(new File(filename), ws);

            // Gets the sheets from workbook
            for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) {
                Sheet s = w.getSheet(sheet);

                bw.write(s.getName());
                bw.newLine();

                Cell[] row = null;

                // Gets the cells from sheet
                for (int i = 0; i < s.getRows(); i++) {
                    row = s.getRow(i);

                    if (row.length > 0) {
                        bw.write(row[0].getContents());
                        for (int j = 1; j < row.length; j++) {
                            bw.write(',');
                            bw.write(row[j].getContents());
                            //System.out.print(",");
                            //System.out.print(row[j].getContents());
                        }
                    }
                    bw.newLine();
                    //System.out.println("");
                }
            }
            bw.flush();
            bw.close();
        } catch (UnsupportedEncodingException e) {
            System.err.println(e.toString());
        } catch (IOException e) {
            System.err.println(e.toString());
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return true;
    }
    
    static public ArrayList<Producto> cargarTxt(int proveedor) {
        final String sep=",";
        final String nomTxt="input.csv";
        String descripcion="Sin descripción";
        
        File txt;
        FileReader fr=null;
        BufferedReader br;
        ArrayList<Producto> lista=null;
        ControlProdServ conprod=new ControlProdServ();
        int n=0;
        try {
            txt = new File(nomTxt);
            fr = new FileReader(txt);
            br = new BufferedReader(fr);
            String linea;
            String datos[];
            System.out.println("Lectura desde "+nomTxt);
            while ((linea = br.readLine()) != null) {
                if (lista==null) {
                    lista=new ArrayList<>();
                }
                if (n>=8) {
                    datos = linea.split(sep);
                    //System.out.println(datos.length);
                    if (datos.length>=5) {
                        
                        
//                        for (int i = 0; i < datos.length; i++) {
//                            System.out.print(datos[i] + "$");
//                        }
//                        System.out.println("");
                        
                        int stock = 0;
                        try {
                            stock = Integer.parseInt(datos[2].trim());
                            //conprod.insertarProd(datos[1], descripcion, 0.0f, stock, "1");
                            conprod.importarProd(datos[1], stock, proveedor);
                        } catch (NumberFormatException numberFormatException) {
                            System.out.println("error"+datos[2]+" -->"+n);
                        } catch(Exception ex){
                            ex.printStackTrace();
                        }
                        
                        //System.out.println(" --> "+datos.length);
                        //lista.add(new Cliente(datos[0],datos[1], datos[2],datos[3],datos[4]));
                    }
                }
                n++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fr) {
                    fr.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        System.out.println("Fin");
        return lista;
    }
}

